import asyncio
import json
import time

from loguru import logger

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.util import async_request as requests
from com.arcfox.yiqing.parser.yiqing_parser import YiQingParser
from com.arcfox.yiqing.spider.yiqing_base_spider import YiQingBaseSpider
from com.arcfox.yiqing.task.yiqing_task import YiQingTask


class CityCodeSpider(YiQingBaseSpider):

    def __init__(self):
        super().__init__()
        self.url = "https://bmfw.www.gov.cn/bjww/interface/interfaceJson"
        self.parser = YiQingParser()
        self.task = YiQingTask()

    async def _pull_task(self):
        return True, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        self.cur_timestamp = str(int(time.time()))
        base_params = self.get_base_params(self.cur_timestamp)
        base_params.update({
            'key': "243D215B2CA449ECABF1E6C93B7D973C",
            'flag': "11"
        })
        try:
            response = await requests.post_with_session(self.url, headers=self.get_headers(),
                                                        data=json.dumps(base_params), timeout=5)
            result_json = response.json()
            logger.info(result_json)
            if result_json['code'] == 0:
                data_list = []
                items = result_json['data']["list"]
                await self.parser.remove_old_city_code()
                self.parser.parse_city_code(data_list, items, "", "", "")
                for data in data_list:
                    await self.parser.save_city_code(data)
        except Exception as e:
            logger.error(e)


if __name__ == "__main__":
    asyncio.run(CityCodeSpider().start_crawl())
